import storage from "/src/components/node-kantboot/libs/storage";
import request from "/src/components/node-kantboot/libs/request";

let result = {};

/**
 * 获取商品分类（树形结构）
 */
result.requestAllHasChildren = () => {
    // 获取用户自身信息
    return new Promise((resolve, reject) => {
        request.send({
            // 检测到未登录，不自动跳转登录页
            isJumpLogin: false,
            uri: "/business-shop-web/shopGoodsType/getAllHasChildren",
            stateSuccess: (res) => {
                console.debug("获取商品分类成功", res);
                storage.set("shopGoodsType:allHasChildren", res.data);
                resolve(res);
            },
            stateFail: (res) => {
                // 如果网络错误
                if (res?.stateCode === "networkError") {
                    uni.showToast({
                        title: "网络错误",
                        icon: "none"
                    });
                }

                reject(res);
            }
        })
    });
}

/**
 * 获取商品分类（树形结构）
 */
result.getAllHasChildren = () => {
    return storage.get("shopGoodsType:allHasChildren");
}

/**
 * 获取所有商品分类的国际化非树形结构
 */
result.getI18nAll = () => {
    return storage.get("shopGoodsTypeI8n:all");
}


/**
 * 请求商品分类的国际化（树形结构）
 */
result.requestI18nAll = () => {
    return new Promise((resolve, reject) => {
        request.send({
            uri: "/business-shop-web/shopGoodsTypeI18n/getAll",
            stateSuccess: (res) => {
                console.debug("获取商品分类成功", res);
                storage.set("shopGoodsTypeI8n:all", res.data);
                let map = {};
                res.data.forEach((item) => {
                    map[item.id+""] = item;
                });
                storage.set("shopGoodsTypeI8n:map", map);
                resolve(res);
            },
            stateFail: (res) => {
                reject(res);
            }
        })
    });
}

/**
 * 根据商品分类id获取国际化
 */
result.getI18nByGoodsTypeId = (goodsTypeId) => {
    goodsTypeId = goodsTypeId + "";
    let map = storage.get("shopGoodsTypeI8n:map");
    return map[goodsTypeId];
}

result.requestParamI18nAll = () => {
    return new Promise((resolve, reject) => {
        request.send({
            uri: "/business-shop-web/shopGoodsParamI18n/getAll",
            stateSuccess: (res) => {
                console.debug("获取商品分类参数国际化成功", res);
                storage.set("shopGoodsParamI18n:all", res.data);
                let map = {};
                res.data.forEach((item) => {
                    map[item.id+""] = item;
                });
                storage.set("shopGoodsParamI18n:map", map);
                resolve(res);
            },
            stateFail: (res) => {
                reject(res);
            }
        })
    });
}

result.getParamI18nAll = () => {
    return storage.get("shopGoodsParamI18n:all");
}

result.getParamI18nByGoodsParamId = (goodsParamId) => {
    goodsParamId = goodsParamId + "";
    let map = storage.get("shopGoodsParamI18n:map");
    return map[goodsParamId];
}

result.requestAttrI18nAll = () => {
    return new Promise((resolve, reject) => {
        request.send({
            uri: "/business-shop-web/shopGoodsAttrI18n/getAll",
            stateSuccess: (res) => {
                console.debug("获取商品分类属性国际化成功", res);
                storage.set("shopGoodsAttrI18n:all", res.data);
                let map = {};
                res.data.forEach((item) => {
                    map[item.id+""] = item;
                });
                storage.set("shopGoodsAttrI18n:map", map);
                resolve(res);
            },
            stateFail: (res) => {
                reject(res);
            }
        })
    });
}

result.getAttrI18nAll = () => {
    return storage.get("shopGoodsAttrI18n:all");
}

result.getAttrI18nByGoodsAttrId = (goodsAttrId) => {
    goodsAttrId = goodsAttrId + "";
    let map = storage.get("shopGoodsAttrI18n:map");
    return map[goodsAttrId];
}


export default result;